package com.carefree.lxwy.manager.service.impl;


import com.carefree.lxwy.manager.mapper.SysRoleMapper;
import com.carefree.lxwy.manager.mapper.SysRoleUserMapper;
import com.carefree.lxwy.manager.service.SysRoleService;
import com.carefree.lxwy.model.dto.system.SysRoleDto;
import com.carefree.lxwy.model.entity.system.SysRole;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class SysRoleServiceImpl implements SysRoleService {

    @Autowired
    private SysRoleMapper sysRoleMapper;

    @Autowired
    private SysRoleUserMapper sysRoleUserMapper;

    @Override
    public PageInfo<SysRole> findByPage(SysRoleDto sysRoleDto, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum , pageSize) ;
        List<SysRole> sysRoleList = sysRoleMapper.findByPage(sysRoleDto) ;
        PageInfo<SysRole> pageInfo = new PageInfo(sysRoleList) ;
        return pageInfo;
    }

    @Override
    public void saveSysRole(SysRole sysRole) {
        sysRoleMapper.save(sysRole);
    }

    @Override
    public void updateSysRole(SysRole sysRole) {
        sysRoleMapper.updateSysRole(sysRole) ;
    }

    @Override
    public void deleteById(Long roleId) {
        sysRoleMapper.deleteById(roleId) ;
    }

    @Override
    public Map<String, Object> findAllRoles(Long userId) {
        List<SysRole> sysRoleList = sysRoleMapper.findAllRoles() ;

        // 根据用户id查询分配的idl列表
        List<Long> roleIds = sysRoleUserMapper.selectRoleIdsByUserId(userId);

        Map<String , Object> map = new HashMap<>();
        map.put("allRolesList" , sysRoleList);
        map.put("sysUserRoles" , roleIds);
        return map;
    }

}
